﻿using System;
using System.Data;
using System.Configuration;
using System.IO;
using System.Xml;


namespace Smg.XML
{
    /// <summary>
    /// xmlOp 类提供对XML文件的读/写/查询/更新/添加/删除 等操作！
    /// </summary>
    public class xmlOperatror
    {
        //声明一个XmlDocument空对象
        protected XmlDocument doc = new XmlDocument();
        public xmlOperatror(string xmlFile)
        {
            doc.Load(GetXmlFilePath(xmlFile));//载入Xml文档
        }
        //保存文件
        public void Save(string filePath)
        {
            doc.Save(GetXmlFilePath(filePath));
        }
        //属性查询，返回属性值
        public string SelectAttrib(string XmlPathNode, string Attrib)
        {
            string _strNode = "";
            _strNode = doc.SelectSingleNode(XmlPathNode).Attributes[Attrib].Value;
            return _strNode;
        }
        // 添加属性
        public bool InsertAttrib(string MainNode, string Attrib, string AttribContent)
        {
            try
            {
                XmlElement objNode = (XmlElement)doc.SelectSingleNode(MainNode); //强制转化成XmlElement对象
                objNode.SetAttribute(Attrib, AttribContent);    //XmlElement对象添加属性方法    
                return true;
            }
            catch
            {
                return false;
            }
        }
        //节点值查询与判断
        public bool SelectNode(string XmlPathNode, int index, string NodeText)
        {
            XmlNodeList _NodeList = doc.SelectNodes(XmlPathNode);
            //循环遍历节点，查询是否存在该节点
            for (int i = 0; i < _NodeList.Count; i++)
            {
                if (_NodeList[i].ChildNodes[index].InnerText == NodeText)
                {
                    return true;
                }
            }
            return false;
        }
        //获取子节点个数
        public int NodeCount(string XmlPathNode)
        {
            int i = 0;
            try
            {
                i = doc.SelectSingleNode(XmlPathNode).ChildNodes.Count;
            }
            catch
            {
                i = 0;
            }
            return i;
        }
        //插入一个节点，带N个子节点
        public bool InsertNode(string MainNode, string ChildNode, string[] Element, string[] Content)
        {
            try
            {
                XmlNode objRootNode = doc.SelectSingleNode(MainNode);    //声明XmlNode对象
                XmlElement objChildNode = doc.CreateElement(ChildNode);  //创建XmlElement对象
                objRootNode.AppendChild(objChildNode);
                for (int i = 0; i < Element.Length; i++)    //循环插入节点元素
                {
                    XmlElement objElement = doc.CreateElement(Element[i]);
                    objElement.InnerText = Content[i];
                    objChildNode.AppendChild(objElement);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
        // 根据Xml文件的节点路径，返回一个DataSet数据集
        public DataSet GetXmlAsDs(string XmlPathNode)
        {
            DataSet ds = new DataSet();
            try
            {
                StringReader read = new StringReader(doc.SelectSingleNode(XmlPathNode).OuterXml);
                ds.ReadXml(read);   //利用DataSet的ReadXml方法读取StringReader文件流
                read.Close();
            }
            catch
            { }
            return ds;
        }
        // 返回Xml文件实际路径
        public string GetXmlFilePath(string xmlFile)
        {
            return xmlFile;
        }
    }
}
